package com.example.model;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.controller.*;
import com.example.model.DatabaseAccess.ExecutionType;

public class ServiceStatusCRUD implements iCRUD<ServiceStatus, Integer, Integer> {

	@Override
	public void create(ServiceStatus instance) {

		HttpPost httpPost = new HttpPost("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceStatusTask/helpConnectServiceStatus");
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("serviceID", Integer.toString(instance.getServiceID())));
		nameValuePairs.add(new BasicNameValuePair("serviceOwnerID", Integer.toString(instance.getServiceOwnerID())));
		nameValuePairs.add(new BasicNameValuePair("RequesterUserID", Integer.toString(instance.getRequesterUserID())));
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		} catch(UnsupportedEncodingException e) { 
			e.printStackTrace();
		}
		
		DataAccessBundle dab = new DataAccessBundle(httpPost, ExecutionType.CREATE);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		dba.doInBackground(dabArr);
		
	}

	@Override
	public ServiceStatus read(Integer ID) {

		HttpGet httpGet = new HttpGet("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceStatusTask/" + Integer.toString(ID));
		
		String result = null;
		
		DataAccessBundle dab = new DataAccessBundle(httpGet, ExecutionType.READ);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		result = dba.doInBackground(dabArr);
		
		ServiceStatus serviceStatus = new ServiceStatus(new ServiceStatusCRUD());
		
		if(result != null)
		{
			try {
				JSONObject record = new JSONObject(result);
				System.out.println(record.toString());
				//System.out.println(Integer.toString(jSonArray.length()));
				if(record != null){
					
					serviceStatus.setServiceStatusID(record.getInt("ServiceStatusID"));
					serviceStatus.setServiceID(record.getInt("ServiceID"));
					serviceStatus.setServiceOwnerID(record.getInt("ServiceOwnerID"));
					serviceStatus.setRequesterUserID(record.getInt("RequesterUserID"));
					if(record.get("IsConnected").equals("1")){
						serviceStatus.setConnected(true);
					}
					else
					{
						serviceStatus.setConnected(false);
					}
					if(record.get("IsPaid").equals("1")){
						serviceStatus.setPaid(true);
					}
					else
					{
						serviceStatus.setPaid(false);
					}
					serviceStatus.setTimeStamp((record.getString("TimeStamp") == "null" || record.getString("TimeStamp") == "") ? "": record.getString("TimeStamp"));
					
				}
			} catch (JSONException e) {
				
				e.printStackTrace();
			}
		}
		
		return serviceStatus;
		
	}

	@Override
	public List<ServiceStatus> readAll() {

		HttpGet httpGet = new HttpGet("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceStatusTask");
		
		String result = null;
		
		DataAccessBundle dab = new DataAccessBundle(httpGet, ExecutionType.READ);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		result = dba.doInBackground(dabArr);
		
		List<ServiceStatus> serviceStatusList = new ArrayList<ServiceStatus>();
		
		if(result != null)
		{
			//System.out.println(result);
			try {
				JSONArray jSonArray = new JSONArray(result);
				//System.out.println(jSonArray.toString());
				//System.out.println(Integer.toString(jSonArray.length()));
				if(jSonArray.length() > 0){
					for(int i = 0; i < jSonArray.length(); i++){
						ServiceStatus serviceStatus = new ServiceStatus(new ServiceStatusCRUD());
						JSONObject record = jSonArray.getJSONObject(i);
						
						serviceStatus.setServiceStatusID(record.getInt("ServiceStatusID"));
						serviceStatus.setServiceID(record.getInt("ServiceID"));
						serviceStatus.setServiceOwnerID(record.getInt("ServiceOwnerID"));
						serviceStatus.setRequesterUserID(record.getInt("RequesterUserID"));
						if(record.get("IsConnected").equals("1")){
							serviceStatus.setConnected(true);
						}
						else
						{
							serviceStatus.setConnected(false);
						}
						if(record.get("IsPaid").equals("1")){
							serviceStatus.setPaid(true);
						}
						else
						{
							serviceStatus.setPaid(false);
						}
						serviceStatus.setTimeStamp((record.getString("TimeStamp") == "null" || record.getString("TimeStamp") == "") ? "": record.getString("TimeStamp"));
						
						serviceStatusList.add(serviceStatus);
						
					}
				}
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		return serviceStatusList;
		
	}

	@Override
	public void update(ServiceStatus instance) {
		System.out.println(instance.getServiceStatusID());
		
		HttpPost httpPut = new HttpPost("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceStatusTask/" + Integer.toString(instance.getServiceStatusID()));
		httpPut.addHeader("X-HTTP-Method-Override", "PUT");
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		if(instance.isConnected()){
			nameValuePairs.add(new BasicNameValuePair("isConnected", "1"));
		}
		else
		{
			nameValuePairs.add(new BasicNameValuePair("isConnected", "0"));
		}
		if(instance.isPaid()){
			nameValuePairs.add(new BasicNameValuePair("isPaid", "1"));
		}
		else
		{
			nameValuePairs.add(new BasicNameValuePair("isPaid", "0"));
		}
		try {
			httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		} catch (UnsupportedEncodingException e) { 
			e.printStackTrace();
		}
		
		DataAccessBundle dab = new DataAccessBundle(httpPut, ExecutionType.UPDATE);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		dba.doInBackground(dabArr);
		
	}

	@Override
	public void delete(Integer ID) {
		HttpDelete httpDelete = new HttpDelete("http://m2.cip.gatech.edu/developer/mobilesquared/widget/base_widget/content/api/helpConnectServiceStatusTask/" + Integer.toString(ID));

		DataAccessBundle dab = new DataAccessBundle(httpDelete, ExecutionType.DELETE);
		DataAccessBundle[] dabArr = {dab};
		
		DatabaseAccess dba = new DatabaseAccess();
		dba.doInBackground(dabArr);
		
	}



}
